package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags="用户管理接口")
@RequestMapping("/users")
@RestController
@Slf4j
@RequiredArgsConstructor
//还没有直接注入方便
public class UserController {
//    @Autowired
    private final IUserService userService;


    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        //Dto转po
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }
//
//    @ApiOperation("查询用户接口")
//    @GetMapping("{id}")
//    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
//        User user = userService.getById(id);
//        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
//        return userVO;
//    }
//
    @ApiOperation("查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        return userService.getUserInfoAndAddressInfo(id);
    }
//    @ApiOperation("批量查询用户接口")
//    @GetMapping
//    public List<UserVO> queryUserById(@ApiParam("用户id") @RequestParam("ids") List<Long> ids) {
//        List<User> users = userService.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class);
//    }
//    @ApiOperation("扣除用户余额接口")
//    @PutMapping("/{id}/reduction/{amount}")
//    public void ReductionUserById(@PathVariable("id") Long id,@PathVariable("amount") Float amount) {
//        log.info(id.toString());
//        userService.reductionUserById(id,amount);
//    }
//

    @ApiOperation("批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@RequestParam("ids") List<Long> ids) {
        return userService.queryListUser(ids);
    }

    @ApiOperation("扣除用户余额接口")
    @PutMapping("/{id}/reduction/{amount}")
    public void ReductionUserById(@PathVariable("id") Long id,@PathVariable("amount") Float amount) {
        userService.reductionUserById(id,amount);
    }

    @ApiOperation("复杂查询条件的用户查询")
    @GetMapping("/list")
    public List<UserVO> fixCondQueryUser(UserQuery userQuery) {
        String name = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer maxBalance = userQuery.getMaxBalance();
        Integer minBalance = userQuery.getMinBalance();
        List<User> user = userService.lambdaQuery()
                .like(userQuery.getName() != null, User::getUsername, name)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        return BeanUtil.copyToList(user,UserVO.class);
    }


    @ApiOperation("复杂分页的用户查询")
    @GetMapping("/pages")
    public PageDTO<UserVO> fixPageQueryUser(UserQuery userQuery) {
        return userService.getFixUserQuery(userQuery);
    }


}


